package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwPaymentReturn;
import com.ys.entity.finance.TcwPaymentReturnItem;
import com.ys.service.finance.TcwPaymentReturnService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/web/finance/tcwPaymentReturn")
public class WebTcwPaymentReturnController {


	@Autowired
	private CommonApiService<TcwPaymentReturn> commonApiService;

	@Autowired
	private TcwPaymentReturnService tcwPaymentReturnService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			Integer paymentReturnType = 2;
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_PaymentReturn);
			TcwPaymentReturn tcwPaymentReturn = new TcwPaymentReturn()
					.setStatus(1)
					.setBid(bid)
					.setPaymentReturnType(paymentReturnType)
					.setCreateTime(new Date());

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_PaymentReturn, new ArrayList<Map<String, Object>>());
//			if (paymentReturnType == 0){
//				for (SubGridConfigDto.ColGridDto colGridDto :subGridConfig.getColGridArr()){
//					if (colGridDto.getField() != null && (colGridDto.getField().equals("tkVid")
//							|| colGridDto.getField().equals("tkVyhzh")|| colGridDto.getField().equals("tkVkhh")
//							|| colGridDto.getField().equals("tkVyhzhmc")|| colGridDto.getField().equals("refundableAmount")
//							)){
//						colGridDto.setHide(true);
//					}
//				}
//			}
			return commonApiService.toAdd(model, tcwPaymentReturn, MenuId.TCW_PaymentReturn, null, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			if (params.get("bid") == null){
				return W.error(model, "单号不能为空");
			}

			String bid = (params.get("bid").toString());

			Map entity = tcwPaymentReturnService.getExtraParam(bid);
			entity.remove("createTime");
			if (entity == null){
				return W.error(model, "预付款退回申请不存在!");
			}
			List<Map<String, Object>> sonlist = tcwPaymentReturnService.getItemList(bid);

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_PaymentReturn, sonlist);
//			if (Integer.valueOf(entity.get("paymentReturnType").toString()) == 0){
//				for (SubGridConfigDto.ColGridDto colGridDto :subGridConfig.getColGridArr()){
//					if (colGridDto.getField() != null && (colGridDto.getField().equals("tkVid")
//							|| colGridDto.getField().equals("tkVyhzh")|| colGridDto.getField().equals("tkVkhh")
//							|| colGridDto.getField().equals("tkVyhzhmc")|| colGridDto.getField().equals("refundableAmount")
//					)){
//						colGridDto.setHide(true);
//					}
//				}
//			}

			return commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TcwPaymentReturn(), MenuId.TCW_PaymentReturn, entity, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

}
